﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.CommonFilter;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Core.Report.PlugIn;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using Kingdee.K3.FIN.App.Core;
using Kingdee.K3.FIN.CB.App.Core;
using Kingdee.K3.FIN.CB.App.Core.CostCal;
using Kingdee.K3.FIN.CB.Common.BusinessEntity;
using Kingdee.K3.FIN.CB.Common.BusinessEntity.FilterCondition;
using Kingdee.K3.FIN.CB.Common.Core;
using Kingdee.K3.FIN.Core;
using Kingdee.K3.FIN.Core.Object;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
namespace RK.ReportPlugin.Local
{
    public abstract class AbstractHSRelatedRpt: CBReportBaseService
    {
        protected bool IsShowFBillType;

        protected List<FilterRow> FilterRows = new List<FilterRow>();

        protected string FilterString = string.Empty;

        protected bool IsUpdateSrcBillInfo;

        protected int PriceDigits
        {
            get;
            set;
        }

        protected HSRelatedRptCondition FilterCondition
        {
            get;
            set;
        }

        protected HSRelatedRpt HSRelatedRptEnum
        {
            get;
            set;
        }

        public AbstractHSRelatedRpt()
        {
            FilterCondition = (HSRelatedRptCondition)(object)new HSRelatedRptCondition();
        }

        protected override void InitCBParams()
        {
            //IL_0007: Unknown result type (might be due to invalid IL or missing references)
            //IL_0011: Expected O, but got Unknown
            base.InitCBParams();
            FilterCondition = (HSRelatedRptCondition)(object)new HSRelatedRptCondition();
        }

        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            base.BuilderReportSqlAndTempTable(filter, tableName);
            ConvertCustomFilterToCondition(filter, (AbstractFilterCondition)(object)FilterCondition);
            PriceDigits = BillExtension.GetValue<int>(filter.ParameterData, "FPRICEDIGITS");
            IsUpdateSrcBillInfo = Convert.ToBoolean(Convert.ToInt16(CommonFunction.GetSystemProfile(((AbstractSysReportServicePlugIn)this).Context, 0L, "CB", "CB_UPDATESRCBILLINFO", "0")));
            Dictionary<string, KeyAlaises> dataFieldByCfg = GetDataFieldByCfg();
            string tempTableName = CommonFunction.GetTempTableName(((AbstractSysReportServicePlugIn)this).Context);
            try
            {
                CreateTempTableByFields(tempTableName, dataFieldByCfg);
                CreateTempTableIndex(tempTableName);
                GetBillData(tempTableName, dataFieldByCfg);
                RemoveNoNeededData(tempTableName);
                TransferDataToRptTable(tempTableName, tableName, dataFieldByCfg);
                CommonFunction.GetAnalyzeTableStat(((AbstractSysReportServicePlugIn)this).Context, tableName);
            }
            finally
            {
                CommonFunction.DropTempTable(((AbstractSysReportServicePlugIn)this).Context, tempTableName, true);
            }
        }

        public override List<long> GetOrgIdList(IRptParams filter)
        {
            if (!((AbstractFilterCondition)FilterCondition).IsValid())
            {
                return ((AbstractSysReportServicePlugIn)this).GetOrgIdList(filter);
            }
            List<long> list = new List<long>();
            list.Add(((AbstractFilterCondition)FilterCondition).AcntOrgId);
            return list;
        }

        public override void ConvertCustomFilterToCondition(IRptParams filter, AbstractFilterCondition condition)
        {
            //IL_005b: Unknown result type (might be due to invalid IL or missing references)
            HSRelatedRptCondition val = condition as HSRelatedRptCondition;
            if (filter != null && filter.FilterParameter != null && filter.FilterParameter.CustomFilter != null && val != null)
            {
                base.ConvertCustomFilterToCondition(filter, (AbstractFilterCondition)(object)val);
                DynamicObject customFilter = filter.FilterParameter.CustomFilter;
                val.Year = (BillExtension.GetValue<int>(customFilter, "Year"));
                val.Period = (BillExtension.GetValue<int>(customFilter, "Period"));
                val.HSRelatedRptEnum = (HSRelatedRptEnum);
                val.IsShowDetailByExpense = (BillExtension.GetValue<bool>(customFilter, "ISSHOWDETAIL", false));
                JSONObject periodDateNext = CommonFunction.GetPeriodDateNext(((AbstractSysReportServicePlugIn)this).Context, ((AbstractFilterCondition)val).AcntPolicyId, val.Year, val.Period);
                if (periodDateNext != null)
                {
                    val.StartDate = (Convert.ToDateTime(periodDateNext.Get("startDate")));
                    val.EndDate = (Convert.ToDateTime(periodDateNext.Get("endDate")));
                }
                SetCostCenterRange(customFilter, val);
                if (BillExtension.GetValue<long>(customFilter, "STARTEXPENSEID_Id") > 0)
                {
                    val.StartExpenseItemNo = ((customFilter["STARTEXPENSEID"] as DynamicObject)["Number"].ToString());
                }
                if (BillExtension.GetValue<long>(customFilter, "ENDEXPENSEID_Id") > 0)
                {
                    val.EndExpenseItemNo = ((customFilter["ENDEXPENSEID"] as DynamicObject)["Number"].ToString());
                }
                if (BillExtension.GetValue<long>(customFilter, "STARTMATERIALID_Id") > 0)
                {
                    val.StartProductNumber = ((customFilter["STARTMATERIALID"] as DynamicObject)["Number"].ToString());
                }
                if (BillExtension.GetValue<long>(customFilter, "ENDMATERIALID_Id") > 0)
                {
                    val.EndProductNumber = ((customFilter["ENDMATERIALID"] as DynamicObject)["Number"].ToString());
                }
                val.StartBillNumber = (BillExtension.GetValue<string>(customFilter, "STARTBILLNO"));
                val.EndBillNumber = (BillExtension.GetValue<string>(customFilter, "ENDBILLNO"));
                val.StartBillNo = (BillExtension.GetValue<string>(customFilter, "STARTPROORDERID"));
                val.EndBillNo = (BillExtension.GetValue<string>(customFilter, "ENDPROORDERID"));
                string value = BillExtension.GetValue<string>(customFilter, "MULBILLTYPE");
                val.BillFormIds = (GetBillFormIdsBySelected(value));
                if (this is CompletionQtyRpt)
                {
                    val.CongfigFilter = (CBAppCommFunction.GetStockInConfigFilter(((AbstractSysReportServicePlugIn)this).Context, string.Empty, false));
                }
                FilterCondition = val;
            }
        }

        protected virtual void SetCostCenterRange(DynamicObject dynamicObject, HSRelatedRptCondition filterCondition)
        {
            if (BillExtension.GetValue<long>(dynamicObject, "STARTCOSTCID_Id") > 0)
            {
                filterCondition.StartCostCenterNo = ((dynamicObject["STARTCOSTCID"] as DynamicObject)["Number"].ToString());
            }
            if (BillExtension.GetValue<long>(dynamicObject, "ENDCOSTCID_Id") > 0)
            {
                filterCondition.EndCostCenterNo = ((dynamicObject["ENDCOSTCID"] as DynamicObject)["Number"].ToString());
            }
        }

        public virtual Dictionary<string, KeyAlaises> GetDataFieldByCfg()
        {
            Dictionary<string, KeyAlaises> dictionary = new Dictionary<string, KeyAlaises>();
            string empty = string.Empty;
            string empty2 = string.Empty;
            KeyAlaises val = (KeyAlaises)(object)new KeyAlaises();
            empty = GetAlias("FCargoOwnerId", "FNAME");
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FNAME", empty, $" {empty} nvarchar(255) null default ''"));
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FCargoOwnerId", "FCargoOwnerId", string.Format(" {0} int null ", "FCargoOwnerId")));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FCargoOwnerId", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FBillFromId", "FBillFromId", string.Format(" {0} varchar(255) null default ''", "FBillFromId")));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FBillFromId", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            empty = GetAlias("FBillTypeId", "FNAME");
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FBillTypeId", "FBillTypeId", string.Format(" {0} varchar(255) not null default ''", "FBillTypeId")));
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FNAME", empty, $" {empty} nvarchar(255) null default ''"));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FBillTypeId", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FBILLIDKEY", "FBILLIDKEY", string.Format(" {0} int not null default 0", "FBILLIDKEY")));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FBILLIDKEY", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FEntryIdKey", "FEntryIdKey", string.Format(" {0} int not null default 0", "FEntryIdKey")));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FEntryIdKey", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FBILLNOKEY", "FBILLNOKEY", string.Format(" {0} nvarchar(255) null default ''", "FBILLNOKEY")));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FBILLNOKEY", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FOrderNumber", "FOrderNumber", string.Format(" {0} nvarchar(255) null default ''", "FOrderNumber")));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FOrderNumber", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FOrderNumberSeq", "FOrderNumberSeq", string.Format(" {0} int not null default 0", "FOrderNumberSeq")));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FOrderNumberSeq", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FORDERENTRYID", "FORDERENTRYID", string.Format(" {0} int not null default 0", "FORDERENTRYID")));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FORDERENTRYID", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            empty = GetAlias("FMaterialField", "FNAME");
            empty2 = GetAlias("FMaterialField", "FNUMBER");
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FMaterialField", "FMaterialField", string.Format(" {0} int not null default 0", "FMaterialField")));
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FNAME", empty, $" {empty} nvarchar(255) null default ''"));
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FNUMBER", empty2, $" {empty2} nvarchar(255) null default ''"));
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FSPECIFICATION", "FSPECIFICATION", string.Format(" {0} nvarchar(1200) null default ''", "FSPECIFICATION")));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FMaterialField", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            empty = GetAlias("FStockField", "FNAME");
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FNAME", empty, $" {empty} nvarchar(255) null default ''"));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FStockField", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            empty = GetAlias("FStockLocField", "FNAME");
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FStockLocField", "FStockLocField", string.Format(" {0} int null default 0", "FStockLocField")));
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FNAME", empty, $" {empty} nvarchar(1024) null default ''"));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FStockLocField", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FDEPTFIELD", "FDEPTFIELD", string.Format(" {0} int not null default 0", "FDEPTFIELD")));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FDEPTFIELD", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            empty2 = GetAlias("FBOMNumberField", "FNUMBER");
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FNUMBER", empty2, $" {empty2} nvarchar(510) null "));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FBOMNumberField", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            empty = GetAlias("FBatchField", "FNAME");
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FNAME", empty, $" {empty} nvarchar(510) null "));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FBatchField", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            empty = GetAlias("FSTOCKSTATUSFIELD", "FNAME");
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FNAME", empty, $" {empty} nvarchar(255) null default ''"));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FSTOCKSTATUSFIELD", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            empty = GetAlias("FAidPropertyField", "FNAME");
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FAidPropertyField", "FAidPropertyField", string.Format(" {0} int null default 0", "FAidPropertyField")));
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FNAME", empty, $" {empty} nvarchar(1024) null default ''"));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FAidPropertyField", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            empty = GetAlias("FBASICUNITFIELD", "FNAME");
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FBASICUNITFIELD", "FBASICUNITFIELD", string.Format(" {0} int not null default 0", "FBASICUNITFIELD")));
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FNAME", empty, $" {empty} nvarchar(255) null default ''"));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FBASICUNITFIELD", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FQtyField", "FQtyField", string.Format(" {0} decimal(28,10) not null default 0", "FQtyField")));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FQtyField", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            empty2 = GetAlias("FSupplierField", "FNUMBER");
            empty = GetAlias("FSupplierField", "FNAME");
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FNUMBER", empty2, $" {empty2} nvarchar(255) null default ' '"));
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FNAME", empty, $" {empty} nvarchar(255) null default ' '"));
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FSupplierField", "FSupplierField", string.Format(" {0} int null default 0 ", "FSupplierField")));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FSupplierField", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FSourceBillTypeName", "FSourceBillTypeName", string.Format(" {0} nvarchar(255) null", "FSourceBillTypeName")));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FSourceBillTypeName", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            empty = GetAlias("FSECUNITID", "FNAME");
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FSECUNITID", "FSECUNITID", string.Format(" {0} int null default 0", "FSECUNITID")));
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FNAME", empty, $" {empty} nvarchar(255) null default ''"));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FSECUNITID", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FSECREALQTY", "FSECREALQTY", string.Format(" {0} decimal(28,10) null default 0", "FSECREALQTY")));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dictionary, "FSECREALQTY", val);
            return dictionary;
        }

        public virtual Dictionary<string, string> CreateSpecialFields()
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            dictionary.Add("FPRODUCTIONTYPE", " FPRODUCTIONTYPE nvarchar(80) not null default '',");
            dictionary.Add("FCOSTCENTERID", " FCOSTCENTERID int not null default 0,");
            dictionary.Add("FCOSTCENTERNUMBER", " FCOSTCENTERNUMBER nvarchar(200) not null default '',");
            dictionary.Add("FCOSTCENTERNAME", " FCOSTCENTERNAME nvarchar(200) not null default '',");
            dictionary.Add("FLINEDATATYPE", $" FLINEDATATYPE int not null default {0},");
            return dictionary;
        }

        public virtual List<string> GetBillFormIdsBySelected(string strMulBillType)
        {
            if (ObjectUtils.IsNullOrEmptyOrWhiteSpace((object)strMulBillType))
            {
                return GetDefaultBillFormIds();
            }
            return strMulBillType.Split(',').ToList();
        }

        public virtual List<string> GetDefaultBillFormIds()
        {
            return new List<string>();
        }

        public virtual void GetDataByProOrderCustomFilter(string strTempTable)
        {
        }

        public virtual void InsertTotalByExpenseItem(string strTempTable)
        {
        }

        public virtual void GetUpdateTempTableInfos(string strTempTable)
        {
            List<string> list = new List<string>();
            list.AddRange(UpdateDeptIdInfos(strTempTable));
            list.Add(UpdateCostCenterByDeptId(strTempTable));
            list.AddRange(UpdateProductionTypeByFormId(strTempTable));
            list.Add(UpdateSupplierInfo(strTempTable));
            if (IsShowFBillType)
            {
                list.AddRange(UpdateFBillTypeName(strTempTable));
            }
            list.AddRange(UpdateOtherReportInfos(strTempTable));
            list.Add(UpdateCostCenterForSrcCenter(strTempTable));
            list.Add(UpdateCostCenterInfosById(strTempTable));
            DBUtils.ExecuteBatch(((AbstractSysReportServicePlugIn)this).Context, list, 1);
        }

        private string UpdateCostCenterForSrcCenter(string strTempTable)
        {
            YearPeriod val = (YearPeriod)(object)new YearPeriod();
            val.Year = (FilterCondition.Year);
            val.Period = (FilterCondition.Period);
            YearPeriod val2 = val;
            YearPeriod val3 = null;
            if (FilterCondition.EndYear == 0 || FilterCondition.EndPeriod == 0)
            {
                val3 = val2;
            }
            else
            {
                YearPeriod val4 = (YearPeriod)(object)new YearPeriod();
                val4.Year = (FilterCondition.EndYear);
                val4.Period = (FilterCondition.EndPeriod);
                val3 = val4;
            }
            YearPeriod val5 = (YearPeriod)(object)new YearPeriod();
            val5.Year = (((CommonBillHeadCondition)FilterCondition).CurYear);
            val5.Period = (((CommonBillHeadCondition)FilterCondition).CurPeriod);
            YearPeriod val6 = val5;
            string arg = PublicFunction.ExchangeHistoryTable(val2, val3, val6, "T_HS_OutInStockSeq", "T_HS_OutInStockSeq_H", "V_HS_OutInStockSeq");
            long dimensionIDByAcctPolicy = AppServiceContext.CommonService.GetDimensionIDByAcctPolicy(((AbstractSysReportServicePlugIn)this).Context, ((AbstractFilterCondition)FilterCondition).AcntSystemId, ((AbstractFilterCondition)FilterCondition).AcntOrgId, ((AbstractFilterCondition)FilterCondition).AcntPolicyId);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat(" merge into {0} T0 using(", strTempTable);
            stringBuilder.AppendFormat(" select distinct dime.FCOSTCENTERID,seq.FBILLENTRYID,seq.FBILLID,tm.FBILLFROMID from  {0} seq ", arg);
            stringBuilder.AppendFormat(" inner join t_hs_outacctg ag on ag.FID=seq.FACCTGID ");
            stringBuilder.AppendFormat(" inner join T_CB_PROORDERDIME dime on seq.FPRODUCTDIMEID=dime.FPRODUCTDIMEID ");
            stringBuilder.AppendFormat(" inner join {0} tm on tm.FEntryIdKey=seq.FBILLENTRYID and tm.FBILLFROMID=seq.FBILLFROMID", strTempTable);
            stringBuilder.AppendFormat(" inner join {0} ccenter on ccenter.FCOSTCENTERID=dime.FCOSTCENTERID", "T_CB_COSTCENTER");
            stringBuilder.AppendFormat(" where dime.FPROORDERTYPE='{0}' and tm.FBILLFROMID in('{1}','{2}') and ag.FDIMENSIONID={3}", ((object)(ProOrderType)2).ToString(), "PRD_INSTOCK", "PRD_RetStock", dimensionIDByAcctPolicy);
            if (!ObjectUtils.IsNullOrEmptyOrWhiteSpace((object)FilterCondition.StartCostCenterNo))
            {
                stringBuilder.AppendFormat(" AND ccenter.FNUMBER >= '{0}'", FilterCondition.StartCostCenterNo);
            }
            if (!ObjectUtils.IsNullOrEmptyOrWhiteSpace((object)FilterCondition.EndCostCenterNo))
            {
                stringBuilder.AppendFormat(" AND ccenter.FNUMBER <= '{0}'", FilterCondition.EndCostCenterNo);
            }
            stringBuilder.AppendFormat(" ) T1 on T1.FBILLENTRYID=T0.FEntryIdKey and T0.FBILLFROMID=T1.FBILLFROMID ");
            stringBuilder.AppendFormat(" when matched then update set T0.FCOSTCENTERID = T1.FCOSTCENTERID");
            return stringBuilder.ToString();
        }

        public virtual List<string> UpdateDeptIdInfos(string strTempTable)
        {
            List<string> list = new List<string>();
            list.Add(UpdateCrossOrgBussinessDept(strTempTable));
            return list;
        }

        public virtual string UpdateCostCenterByDeptId(string strTempTable)
        {
            StringBuilder stringBuilder = new StringBuilder();
            if (!ObjectUtils.IsNullOrEmptyOrWhiteSpace((object)FilterCondition.StartCostCenterNo))
            {
                stringBuilder.AppendFormat(" AND ccenter.FNUMBER >= '{0}'", FilterCondition.StartCostCenterNo);
            }
            if (!ObjectUtils.IsNullOrEmptyOrWhiteSpace((object)FilterCondition.EndCostCenterNo))
            {
                stringBuilder.AppendFormat(" AND ccenter.FNUMBER <= '{0}'", FilterCondition.EndCostCenterNo);
            }
            string text = " and t0.FCOSTCENTERID=0";
            return CBAppCommFunction.UpdateCostCenterByDeptIdForSubCostCenter(((AbstractSysReportServicePlugIn)this).Context, strTempTable, stringBuilder.ToString(), text);
        }

        public virtual List<string> UpdateOtherReportInfos(string strTempTable)
        {
            return new List<string>();
        }

        public virtual List<string> UpdateProductionTypeByFormId(string strTempTable)
        {
            string text = " update {0} set FPRODUCTIONTYPE = N'{1}' where FBillFromId='{2}' and flinedatatype={3} ";
            string format = $"{text} and fcostcenterid=0 ";
            List<string> list = new List<string>();
            new StringBuilder();
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_Comm, "PRD_INSTOCK", 0));
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_Comm, "PRD_RetStock", 0));
            list.Add(string.Format(format, strTempTable, base.CBInternationalConst.Producted_WW, "PRD_INSTOCK", 0));
            list.Add(string.Format(format, strTempTable, base.CBInternationalConst.Producted_WW, "PRD_RetStock", 0));
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_Sim, "SP_InStock", 0));
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_Sim, "SP_OUTSTOCK", 0));
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_WW, "STK_InStock", 0));
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_WW, "PUR_MRB", 0));
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_Comm, "PRD_FeedMtrl", 0));
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_Comm, "PRD_PickMtrl", 0));
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_Comm, "PRD_ReturnMtrl", 0));
            list.Add(string.Format(format, strTempTable, base.CBInternationalConst.Producted_WW, "PRD_FeedMtrl", 0));
            list.Add(string.Format(format, strTempTable, base.CBInternationalConst.Producted_WW, "PRD_PickMtrl", 0));
            list.Add(string.Format(format, strTempTable, base.CBInternationalConst.Producted_WW, "PRD_ReturnMtrl", 0));
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_Sim, "SP_PickMtrl", 0));
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_Sim, "SP_ReturnMtrl", 0));
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_WW, "SUB_FEEDMTRL", 0));
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_WW, "SUB_PickMtrl", 0));
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_WW, "SUB_RETURNMTRL", 0));
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_Sim, "REM_INSTOCK", 0));
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_Rem, "REM_OUTSTOCK", 0));
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_Rem, "REM_PickMtrl", 0));
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_Rem, "REM_ReturnMtrl", 0));
            list.Add(string.Format(text, strTempTable, base.CBInternationalConst.Producted_Rem, "REM_TransferDirect", 0));
            return list;
        }

        public virtual void RemoveNoNeededData(string strTempTable)
        {
        }

        public virtual void TransferDataToRptTable(string strTempTable, string tableName, Dictionary<string, KeyAlaises> dataFields)
        {
            List<KeyAlaises> list = dataFields.Select((KeyValuePair<string, KeyAlaises> o) => o.Value).ToList();
            List<string> list2 = new List<string>();
            foreach (KeyAlaises item in list)
            {
                list2.AddRange(((IEnumerable<KeyAlais>)item).Select((KeyAlais o) => $"tmp.{o.Alais}"));
            }
            Dictionary<string, string> source = CreateSpecialFields();
            string text = string.Format("tmp.{0}", "FOrderNumberSeq");
            string text2 = "(case when {0}=0 then '' else CONVERT(VARCHAR(30), {0}) end) AS {1}";
            if (list2.Contains(text))
            {
                list2.Remove(text);
                list2.Add(string.Format(text2, text, "FOrderNumberSeq"));
            }
            GetSpecialColumnsForUpdateToNulls(list2, text2);
            StringBuilder stringBuilder = new StringBuilder();
            if (!ObjectUtils.IsNullOrEmptyOrWhiteSpace((object)FilterString))
            {
                stringBuilder.AppendFormat(" select T.*,{0}", string.Format(base.KSQL_SEQ, GetKSqlSeqOrderFields()));
                stringBuilder.AppendFormat(" into {0}", tableName);
                stringBuilder.AppendFormat(" from (");
            }
            stringBuilder.AppendFormat("SELECT {0},", string.Join(",", list2));
            stringBuilder.AppendFormat(" {0},", string.Join(",", source.Select((KeyValuePair<string, string> o) => $"tmp.{o.Key}")));
            stringBuilder.AppendFormat(" curr.FAMOUNTDIGITS,{0},", (PriceDigits >= 0) ? $" {PriceDigits} as FPRICEDIGITS" : "curr.FPRICEDIGITS");
            stringBuilder.AppendLine(" unit.FPRECISION FQTYDIGITS");
            stringBuilder.AppendFormat(" {0}", AddSpecialReportFields());
            if (ObjectUtils.IsNullOrEmptyOrWhiteSpace((object)FilterString))
            {
                stringBuilder.AppendFormat(" ,{0}", string.Format(base.KSQL_SEQ, GetKSqlSeqOrderFields()));
                stringBuilder.AppendFormat(" INTO {0} ", tableName);
            }
            stringBuilder.AppendFormat(" FROM {0} tmp", strTempTable);
            stringBuilder.AppendFormat(" LEFT JOIN {0} curr ON curr.FCURRENCYID={1} ", "T_BD_CURRENCY", ((AbstractFilterCondition)FilterCondition).CurrencyId);
            stringBuilder.AppendFormat(" LEFT JOIN {0} unit ON tmp.{1}=unit.FUNITID", "T_BD_UNIT", "FBASICUNITFIELD");
            stringBuilder.AppendFormat(" {0}", GetSpecialJoinFields());
            stringBuilder.AppendFormat(" {0}", GetTransWhereSql());
            if (!ObjectUtils.IsNullOrEmptyOrWhiteSpace((object)FilterString))
            {
                stringBuilder.AppendFormat(" ) T where {0}", FilterString);
            }
            FINDBUtils.ExecuteWithTime(((AbstractSysReportServicePlugIn)this).Context, stringBuilder.ToString(), (IEnumerable<SqlParam>)null);
        }

        public virtual string AddSpecialReportFields()
        {
            return string.Empty;
        }

        public virtual string GetSpecialJoinFields()
        {
            return string.Empty;
        }

        public virtual void GetSpecialColumnsForUpdateToNulls(List<string> columnNames, string strQtyToVar)
        {
        }

        public virtual string GetKSqlSeqOrderFields()
        {
            return string.Format("{0} ASC,{1} ASC,{2} ASC", "FBillFromId", "FBILLIDKEY", "FEntryIdKey");
        }

        public virtual string GetTransWhereSql()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat(" where 1=1");
            if (!StringUtils.IsEmpty(FilterCondition.StartCostCenterNo))
            {
                stringBuilder.AppendFormat(" and tmp.FCOSTCENTERNUMBER >='{0}'", FilterCondition.StartCostCenterNo);
            }
            if (!StringUtils.IsEmpty(FilterCondition.EndCostCenterNo))
            {
                stringBuilder.AppendFormat(" and tmp.FCOSTCENTERNUMBER <='{0}'", FilterCondition.EndCostCenterNo);
            }
            return stringBuilder.ToString();
        }

        public virtual void CreateTempTableIndex(string strTempTable)
        {
            string text = string.Format(" CREATE INDEX {0} ON {1}({2})", CommonFunction.GetTempTableIndexName(""), strTempTable, "FEntryIdKey");
            DBUtils.Execute(((AbstractSysReportServicePlugIn)this).Context, text);
        }

        public List<string> GetBillFormsForStockIn(string strMulBillType)
        {
            List<string> list = new List<string>();
            if (ObjectUtils.IsNullOrEmptyOrWhiteSpace((object)strMulBillType))
            {
                return GetDefaultBillFormIds();
            }
            string[] source = strMulBillType.Split(',');
            if (source.Any((string p) => p == ((object)(ProOrderType)0).ToString()))
            {
                list.Add("PRD_INSTOCK");
                list.Add("PRD_RetStock");
            }
            if (source.Any((string p) => p == ((object)(ProOrderType)1).ToString()))
            {
                list.Add("SP_InStock");
                list.Add("SP_OUTSTOCK");
            }
            if (source.Any((string p) => p == ((object)(ProOrderType)2).ToString()))
            {
                list.Add("STK_InStock");
                list.Add("PUR_MRB");
            }
            if (source.Any((string p) => p == ((object)(ProOrderType)6).ToString()))
            {
                list.Add("REM_INSTOCK");
                list.Add("REM_OUTSTOCK");
            }
            return list;
        }

        public string UpdateProductType(string strTempTable)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat(" merge into {0} T0 using( ", strTempTable);
            stringBuilder.AppendFormat(" select (case when (t1.FPRODUCTTYPE='{0}') ", 1);
            stringBuilder.AppendFormat(" then N'{0}' ", base.CBInternationalConst.MainProduct);
            stringBuilder.AppendFormat(" when (t1.FPRODUCTTYPE='{0}') ", 2);
            stringBuilder.AppendFormat(" then N'{0}' ", base.CBInternationalConst.JoinProduct);
            stringBuilder.AppendFormat(" else N'{0}' end ) AS FPRODUCTTYPE,t1.FEntryID ", base.CBInternationalConst.SecondaryProduct);
            stringBuilder.AppendFormat(" from T_SUB_REQORDERENTRY t1 ) T ");
            stringBuilder.AppendFormat(" ON (T.FEntryID = T0.{0} and T0.FBillFromId IN ('{1}', '{2}')) ", "FORDERENTRYID", "STK_InStock", "PUR_MRB");
            stringBuilder.AppendFormat(" WHEN MATCHED THEN UPDATE SET T0.FPRODUCTTYPE = T.FPRODUCTTYPE ");
            return stringBuilder.ToString();
        }

        public string UpdatePrdProductType(string strTempTable, string formId)
        {
            string arg = "T_PRD_INSTOCKENTRY";
            if (formId == "PRD_RetStock")
            {
                arg = "T_PRD_RESTOCKENTRY";
            }
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat(" merge into {0} T0 using( ", strTempTable);
            stringBuilder.AppendFormat(" select (case when (t1.FPRODUCTTYPE='{0}') ", 1);
            stringBuilder.AppendFormat(" then N'{0}' ", base.CBInternationalConst.MainProduct);
            stringBuilder.AppendFormat(" when (t1.FPRODUCTTYPE='{0}') ", 2);
            stringBuilder.AppendFormat(" then N'{0}' ", base.CBInternationalConst.JoinProduct);
            stringBuilder.AppendFormat(" else N'{0}' end ) AS FPRODUCTTYPE,t1.FEntryID ", base.CBInternationalConst.SecondaryProduct);
            stringBuilder.AppendFormat(" from {0} t1 )T", arg);
            stringBuilder.AppendFormat(" ON (T.FEntryID = T0.FEntryIdKey and T0.FBillFromId = '{0}') WHEN MATCHED THEN UPDATE SET T0.FPRODUCTTYPE = T.FPRODUCTTYPE ", formId);
            return stringBuilder.ToString();
        }

        public List<string> UpdateSrcBillInfoForInstock(string strTempTable)
        {
            return CBAppCommFunction.UpdateSrcBillInfoForInstock(((AbstractSysReportServicePlugIn)this).Context, strTempTable, "FOrderNumber", "FOrderNumberSeq", "FBillFromId", "FEntryIdKey", "FORDERENTRYID", "");
        }

        public List<string> UpdateSrcBillInfoForMRB(string strTempTable)
        {
            return CBAppCommFunction.UpdateSrcBillInfoForMRB(((AbstractSysReportServicePlugIn)this).Context, strTempTable, "FOrderNumber", "FOrderNumberSeq", "FBillFromId", "FEntryIdKey", "FORDERENTRYID", "");
        }

        public List<string> GetDefaultBillFormIdsForStockIn()
        {
            return CBAppCommFunction.GetStockInBillFormIds(((AbstractSysReportServicePlugIn)this).Context);
        }

        public void CreateTempTableByFields(string strTempTable, Dictionary<string, KeyAlaises> dataFields)
        {
            StringBuilder stringBuilder = new StringBuilder();
            Dictionary<string, string> source = CreateSpecialFields();
            List<KeyAlaises> list = dataFields.Select((KeyValuePair<string, KeyAlaises> o) => o.Value).ToList();
            List<string> list2 = new List<string>();
            foreach (KeyAlaises item in list)
            {
                list2.AddRange(((IEnumerable<KeyAlais>)item).Select((KeyAlais o) => o.Create));
            }
            stringBuilder.AppendFormat("CREATE TABLE {0}", strTempTable);
            stringBuilder.AppendLine(" (");
            stringBuilder.AppendLine(string.Join("", source.Select((KeyValuePair<string, string> o) => o.Value)));
            stringBuilder.AppendLine(string.Join(",", list2));
            stringBuilder.AppendLine(")");
            DBUtils.Execute(((AbstractSysReportServicePlugIn)this).Context, stringBuilder.ToString());
        }

        public void GetBillData(string strTempTable, Dictionary<string, KeyAlaises> dataFields)
        {
            if (((AbstractFilterCondition)FilterCondition).IsValid())
            {
                AcctgBillData val = (AcctgBillData)(object)new AcctgBillData();
                val.GetAcctgBillData(((AbstractSysReportServicePlugIn)this).Context, FilterCondition, dataFields, strTempTable);
                GetDataByProOrderCustomFilter(strTempTable);
                GetUpdateTempTableInfos(strTempTable);
                InsertTotalByExpenseItem(strTempTable);
                CommonFunction.GetAnalyzeTableStat(((AbstractSysReportServicePlugIn)this).Context, strTempTable);
            }
        }

        public string GetAlias(string strKey, string strChild)
        {
            return strKey + "_" + strChild;
        }

        public string UpdateCrossOrgBussinessDept(string tempTable)
        {
            List<long> orgIdsdByAccountOrgIdWithForbidStatus = CommonFunction.GetOrgIdsdByAccountOrgIdWithForbidStatus(((AbstractSysReportServicePlugIn)this).Context, ((AbstractFilterCondition)FilterCondition).AcntSystemId, new List<long>
            {
                ((AbstractFilterCondition)FilterCondition).AcntOrgId
            }, true);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat(" MERGE INTO {0} T0 USING ( ", tempTable);
            stringBuilder.AppendFormat(" SELECT DISTINCT MO.FBILLNO,MOENTRY.FSEQ FROM {0} MO", "T_PRD_MO");
            stringBuilder.AppendFormat(" INNER JOIN {0} MOENTRY ON MO.FID=MOENTRY.FID ", "T_PRD_MOENTRY");
            stringBuilder.AppendFormat(" INNER JOIN {0} TEMPK ON MO.FBILLNO=TEMPK.FORDERNUMBER AND MOENTRY.FSEQ=TEMPK.FORDERNUMBERSEQ ", tempTable);
            stringBuilder.AppendFormat(" WHERE MO.FISENTRUST='1' AND MO.FENTRUSTORGID IN ({0}) AND MO.FPRDORGID NOT IN ({0})", string.Join(",", orgIdsdByAccountOrgIdWithForbidStatus.ToArray()));
            stringBuilder.AppendFormat(" AND TEMPK.FBILLFROMID IN('{0}','{1}','{2}','{3}') AND TEMPK.FLINEDATATYPE=0 ) T", "PRD_INSTOCK", "PRD_FEEDMTRL", "PRD_PICKMTRL", "PRD_ReturnMtrl");
            stringBuilder.AppendFormat(" ON(T.FBILLNO=T0.FORDERNUMBER AND T.FSEQ=T0.FORDERNUMBERSEQ)");
            stringBuilder.AppendFormat(" WHEN MATCHED THEN UPDATE SET T0.FDEPTFIELD=0 ");
            return stringBuilder.ToString();
        }

        public string UpdateSupplierInfo(string strTempTable)
        {
            //IL_0000: Unknown result type (might be due to invalid IL or missing references)
            bool flag = new CBCommonService().IsBaseDataShare(((AbstractSysReportServicePlugIn)this).Context);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat(" merge into {0} T0 using(", strTempTable);
            stringBuilder.AppendFormat(" select DISTINCT tmp.FBillFromId,tmp.FEntryIdKey,tmp.FOrderNumber,tmp.FOrderNumberSeq,");
            stringBuilder.AppendFormat(" sup.FSUPPLIERID ,sup.FNUMBER as FSUPPLIERID_FNUMBER ,sup_l.FNAME as FSUPPLIERID_FNAME");
            stringBuilder.AppendFormat(" FROM {0} tmp ", strTempTable);
            stringBuilder.AppendFormat(" INNER JOIN {0} type1 ON TYPE1.FNUMBER=tmp.FOrderNumber AND type1.FSEQ=tmp.FOrderNumberSeq ", "T_CB_PROORDERTYPE");
            stringBuilder.AppendFormat(" AND type1.FENTRUSTORGID!=0 AND type1.FTRUSTEEORGID!=0");
            stringBuilder.AppendFormat(" INNER JOIN {0} sup ON sup.FCORRESPONDORGID=type1.FTRUSTEEORGID AND sup.FDOCUMENTSTATUS='{1}' {2}", "T_BD_SUPPLIER", (object)(DocumentStatus)3, flag ? "" : " AND sup.FUSEORGID=type1.FENTRUSTORGID");
            stringBuilder.AppendFormat(" LEFT JOIN {0} sup_l ON sup_l.FSUPPLIERID=sup.FSUPPLIERID AND sup_l.FLOCALEID={1}", "T_BD_SUPPLIER_L", ((AbstractSysReportServicePlugIn)this).Context.UserLocale.LCID);
            stringBuilder.AppendFormat(" WHERE tmp.FPRODUCTIONTYPE IN ( N'{0}' ) AND tmp.FSupplierField =0) T", base.CBInternationalConst.Producted_WW);
            stringBuilder.AppendFormat(" on (T0.FEntryIdKey=T.FEntryIdKey AND T0.FBillFromId=T.FBillFromId AND T0.FOrderNumber = T.FOrderNumber");
            stringBuilder.AppendFormat(" AND T0.FOrderNumberSeq=T.FOrderNumberSeq)");
            stringBuilder.AppendFormat(" when matched then update set T0.FSupplierField = T.FSUPPLIERID,T0.FSupplierField_FNUMBER=T.FSUPPLIERID_FNUMBER,T0.FSupplierField_FNAME=T.FSUPPLIERID_FNAME");
            return stringBuilder.ToString();
        }

        public List<string> UpdateFBillTypeName(string strTempTable)
        {
            List<string> list = new List<string>();
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat(" merge into {0} T0 using(", strTempTable);
            stringBuilder.AppendFormat(" select DISTINCT tmp.FBillFromId,tmp.FEntryIdKey,tmp.FOrderNumber,max(btype_l.FNAME) as FBILLTYPENAME");
            stringBuilder.AppendFormat(" FROM {0} tmp", strTempTable);
            stringBuilder.AppendFormat(" INNER JOIN T_CB_PROORDERTYPE type1 ON TYPE1.FNUMBER=tmp.FOrderNumber AND type1.FSEQ=tmp.FOrderNumberSeq AND type1.FPROORDERTYPE='PO'");
            stringBuilder.AppendFormat(" LEFT JOIN T_BAS_BILLTYPE_L btype_l ON btype_l.FBILLTYPEID=type1.FBILLTYPEID AND btype_l.FLOCALEID={0}", ((AbstractSysReportServicePlugIn)this).Context.UserLocale.LCID);
            stringBuilder.AppendFormat(" WHERE tmp.FPRODUCTIONTYPE IN ( N'{0}')", base.CBInternationalConst.Producted_Comm);
            stringBuilder.AppendFormat(" GROUP BY tmp.FBillFromId,tmp.FEntryIdKey,tmp.FOrderNumber");
            stringBuilder.AppendFormat(" union all");
            stringBuilder.AppendFormat(" select DISTINCT tmp.FBillFromId,tmp.FEntryIdKey,tmp.FOrderNumber,max(btype_l.FNAME) as FBILLTYPENAME");
            stringBuilder.AppendFormat(" FROM {0} tmp", strTempTable);
            stringBuilder.AppendFormat(" INNER JOIN T_CB_PROORDERTYPE type1 ON TYPE1.FNUMBER=tmp.FOrderNumber AND type1.FSEQ=tmp.FOrderNumberSeq AND type1.FPROORDERTYPE='OUTSRC' AND type1.FFORMID = 'SUB_SUBREQORDER'");
            stringBuilder.AppendFormat(" LEFT JOIN T_BAS_BILLTYPE_L btype_l ON btype_l.FBILLTYPEID=type1.FBILLTYPEID AND btype_l.FLOCALEID={0}", ((AbstractSysReportServicePlugIn)this).Context.UserLocale.LCID);
            stringBuilder.AppendFormat(" WHERE tmp.FPRODUCTIONTYPE IN ( N'{0}') ", base.CBInternationalConst.Producted_WW);
            stringBuilder.AppendFormat(" and tmp.FBillFromId in ('{0}','{1}','{2}','{3}','{4}')", "SUB_FEEDMTRL", "SUB_RETURNMTRL", "PUR_MRB", "SUB_PickMtrl", "STK_InStock");
            stringBuilder.AppendFormat(" GROUP BY tmp.FBillFromId,tmp.FEntryIdKey,tmp.FOrderNumber");
            stringBuilder.AppendFormat(" union all");
            stringBuilder.AppendFormat(" select DISTINCT tmp.FBillFromId,tmp.FEntryIdKey,tmp.FOrderNumber,max(btype_l.FNAME) as FBILLTYPENAME");
            stringBuilder.AppendFormat(" FROM {0} tmp", strTempTable);
            stringBuilder.AppendFormat(" INNER JOIN T_CB_PROORDERTYPE type1 ON TYPE1.FNUMBER=tmp.FOrderNumber AND type1.FSEQ=tmp.FOrderNumberSeq AND type1.FPROORDERTYPE='OUTSRC' AND type1.FFORMID = 'PRD_MO'");
            stringBuilder.AppendFormat(" LEFT JOIN T_BAS_BILLTYPE_L btype_l ON btype_l.FBILLTYPEID=type1.FBILLTYPEID AND btype_l.FLOCALEID={0}", ((AbstractSysReportServicePlugIn)this).Context.UserLocale.LCID);
            stringBuilder.AppendFormat(" WHERE tmp.FPRODUCTIONTYPE IN ( N'{0}')", base.CBInternationalConst.Producted_WW);
            stringBuilder.AppendFormat(" and tmp.FBillFromId in ('{0}','{1}','{2}','{3}','{4}')", "PRD_INSTOCK", "PRD_RetStock", "PRD_FeedMtrl", "PRD_PickMtrl", "PRD_ReturnMtrl");
            stringBuilder.AppendFormat(" GROUP BY tmp.FBillFromId,tmp.FEntryIdKey,tmp.FOrderNumber");
            stringBuilder.AppendFormat(" ) T on T.FEntryIdKey=T0.FEntryIdKey AND T0.FOrderNumber = T.FOrderNumber AND T0.FBillFromId=T.FBillFromId");
            stringBuilder.AppendFormat(" when matched then update set T0.FSourceBillTypeName = T.FBILLTYPENAME");
            list.Add(stringBuilder.ToString());
            return list;
        }

        public string UpdateCostCenterInfosById(string strTempTable)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat(" merge into {0} T0 using(", strTempTable);
            stringBuilder.AppendFormat(" select cc.FCOSTCENTERID,cc.FNUMBER as FCOSTCENTERNUMBER ,isnull(ccl.FNAME,N' ') as FCOSTCENTERNAME");
            stringBuilder.AppendFormat(" FROM {0} cc ", "T_CB_COSTCENTER");
            stringBuilder.AppendFormat(" LEFT JOIN {0} ccl ON ccl.FCOSTCENTERID=cc.FCOSTCENTERID AND ccl.FLOCALEID={1}", "T_CB_COSTCENTER_L", ((AbstractSysReportServicePlugIn)this).Context.UserLocale.LCID);
            stringBuilder.AppendFormat(" ) T on T0.FCOSTCENTERID=T.FCOSTCENTERID ");
            stringBuilder.AppendFormat(" when matched then update set T0.FCOSTCENTERNUMBER = T.FCOSTCENTERNUMBER,T0.FCOSTCENTERNAME=T.FCOSTCENTERNAME");
            return stringBuilder.ToString();
        }

        public void UpdateProductUnitAndUnitQty(string strTempTable)
        {
            string text = AppServiceContext.DBService.CreateTemporaryTableName(((AbstractSysReportServicePlugIn)this).Context);
            new List<string>();
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat(" SELECT tmp.FBillFromId,tmp.FEntryIdKey,tmp.FBILLNOKEY,unitprd.FUNITID AS FPRODUCEUNIT,unitlprd.FNAME as FPRODUCEUNITFNAME,tmp.FBILLSEQKEY,");
            stringBuilder.AppendFormat(" ROW_NUMBER() OVER(ORDER BY tmp.FBillFromId,tmp.FEntryIdKey,tmp.FBILLNOKEY,tmp.FBILLSEQKEY) AS FPRIMARYKEY,");
            stringBuilder.AppendFormat(" mat.FMaterialId,unitlprd.FUNITID AS FSrcUnitFieldName,tmp.FBASICUNITFIELD AS FDstUntiFieldName,tmp.FQTYFIELD,unitprd.FPRECISION {0}", FilterCondition.IsShowDetailByExpense ? " ,tmp.FExpenseItemField" : string.Empty);
            stringBuilder.AppendFormat(" ,tmp.FOrderNumber,tmp.FOrderNumberSeq");
            stringBuilder.AppendFormat(" INTO {0}", text);
            stringBuilder.AppendFormat(" FROM {0} tmp", strTempTable);
            stringBuilder.AppendFormat(" INNER JOIN T_BD_MATERIAL mat ON mat.FMATERIALID=tmp.FMATERIALFIELD");
            stringBuilder.AppendFormat(" INNER JOIN T_BD_MATERIALPRODUCE matprd ON tmp.FMATERIALFIELD = matprd.FMATERIALID");
            stringBuilder.AppendFormat(" INNER JOIN T_BD_UNIT unitprd ON matprd.FPRODUCEUNITID = unitprd.FUNITID");
            stringBuilder.AppendFormat(" LEFT JOIN T_BD_UNIT_L unitlprd ON unitprd.FUNITID = unitlprd.FUNITID AND unitlprd.FLOCALEID = {0}", base.LCID);
            stringBuilder.AppendFormat(" WHERE tmp.FQTYFIELD <> 0");
            DBUtils.Execute(((AbstractSysReportServicePlugIn)this).Context, stringBuilder.ToString());
            CommonFunction.GetAnalyzeTableStat(((AbstractSysReportServicePlugIn)this).Context, text);
            CBCommonService val = (CBCommonService)(object)new CBCommonService();
            val.UpdateQtyByUnit(((AbstractSysReportServicePlugIn)this).Context, text, "FPRIMARYKEY", "FMaterialId", "FSrcUnitFieldName", "FDstUntiFieldName", "FQTYFIELD", "FPRECISION", false);
            stringBuilder.Clear();
            stringBuilder.AppendFormat(" merge into {0} T0 using(", strTempTable);
            stringBuilder.AppendFormat(" SELECT distinct tmp.FBillFromId,tmp.FEntryIdKey,tmp.FBILLNOKEY,tmp.FPRODUCEUNIT,tmp.FPRODUCEUNITFNAME,tmp.FBILLSEQKEY, ");
            stringBuilder.AppendFormat(" tmp.FQTYFIELD AS FPRODUCEUNITQTY {0}", FilterCondition.IsShowDetailByExpense ? " ,tmp.FExpenseItemField" : string.Empty);
            stringBuilder.AppendFormat(" ,tmp.FOrderNumber,tmp.FOrderNumberSeq");
            stringBuilder.AppendFormat(" FROM {0} tmp", text);
            stringBuilder.AppendFormat(" ) T on (T0.FENTRYIDKEY = T.FENTRYIDKEY AND T0.FBILLNOKEY=T.FBILLNOKEY AND T.FBILLSEQKEY=T0.FBILLSEQKEY AND T0.FOrderNumber=T.FOrderNumber AND T0.FOrderNumberSeq=T.FOrderNumberSeq AND T0.FBillFromId=T.FBillFromId {0})", FilterCondition.IsShowDetailByExpense ? " AND T.FExpenseItemField=T0.FExpenseItemField" : string.Empty);
            stringBuilder.AppendFormat(" when matched then update set T0.FPRODUCEUNIT = T.FPRODUCEUNIT,T0.FPRODUCEUNITQTY=T.FPRODUCEUNITQTY,T0.FPRODUCEUNIT_FNAME = T.FPRODUCEUNITFNAME");
            stringBuilder.AppendFormat(" WHERE T0.FQTYFIELD<>0");
            DBUtils.Execute(((AbstractSysReportServicePlugIn)this).Context, stringBuilder.ToString());
            CommonFunction.DropTempTable(((AbstractSysReportServicePlugIn)this).Context, text, true);
        }
    }
}
